/*
 * main.cpp for gen_3d_paths
 *
 *  Created on: Apr 4, 2012
 *      Author: hxu
 */

#include <iostream>
#include <fstream>
#include <time.h>
#include "Toolset.h"
#include <vector>
#include "Circuit3D.h"

using namespace std;
using namespace Cxuh;

//extern int SRCZ; // index of tier of clk src
//extern vector<Cxuh::clockpath> PATH_INFO;	// num_buf for each path in each tier
//extern int LSTAGE; // unit um

void help_info();

int main(int argc, const char* argv[]) {

	string setupname = "";	// setup file and circuit file
	string ofname = "", logname = "";
	string prompt;
	unsigned int pos1, pos2;
	time_t tstart, tend, date;
	double cputime = 0;
	string OUTDIR = "./result";
	string prog_name = argv[0];


	/* determine file name */
	cout << endl << " *** " << prog_name << " ******************* " << endl;
	cout << " ------------------------------------------ " << endl;

	// read in paramters
	if (argc >= 5) {
		for (int i = 1; i <= argc - 1; i++) {
			if (argv[i][0] == '-') {
				// an option
				switch (argv[i][1]) {
				case 's':
					setupname = argv[++i];
					break;
				case 'o':
					ofname = argv[++i];
					break;
				case 'd':
					OUTDIR = argv[++i];
					break;
				}
			} else
				i++; // skip the option
		}
	}


	/* determine file name */
	cout << " * Read file \"" << setupname << "\" as setup file." << endl;

	/* read in setup file */
	ifstream setupstream(setupname.c_str(), ios::in);
	if (!setupstream) {
		cerr << " No setup file \"" << setupname << "\"!" << endl;
		help_info();
		return -1;
	}

	/* define output and log files */
	if (ofname == "") {
		ofname = setupname;
		pos1 = ofname.find(".txt");
		pos2 = ofname.rfind("/", pos1);
		ofname.replace(pos1, sizeof(".txt"), ".out");

		if (pos2 != string::npos) {
			// "/" is found
			ofname.replace(0, pos2, OUTDIR);
		} else
			ofname.insert(0, OUTDIR);
	}

	logname = ofname;
	pos1 = logname.find(".out");
	logname.replace(pos1, sizeof(".txt"), ".log");
	fstream fileout(ofname.c_str());

	if (!fileout)
		prompt = " * Writing to \"" + ofname + "\".\n";
	else
		prompt = " * Overwriting \"" + ofname + "\".\n";

	fileout.close();

	/* CTS */
	try {
		tstart = clock();;
		cout << prompt;

		// output
		date = time(NULL);
		fileout.open(ofname.c_str(), ios::out);
		if(fileout.fail()){
			string errstr = " Cannot write to "+ofname+"!";
			help_info();
			throw(errstr);
		}

		fileout << "# Generated by " << argv[0] << "." << endl;
		fileout << "# Date: " << ctime(&date) << endl << endl;

		// parse

//		Txuh::extractPar(setupstream, fileout);
//		Circuit3D circuit(PATH_INFO, LSTAGE, SRCZ);
		Circuit3D circuit(setupstream, fileout);
		fileout << circuit;

		tend = clock();
		cputime = (tend - tstart) / double(CLOCKS_PER_SEC);
		fileout << endl << "# CPU time: " << cputime << " s." << endl << endl;

		fileout.close();
		setupstream.close();
		cout<<" * Successfully executed!" << endl;
	} catch (string & errinfo) {
		cerr << errinfo << endl;
		return -1;
	} catch (char const *a){
		cerr << a << endl;
		return -1;
	}

	return 0;

}

void help_info() {
	cout << "  Help" << endl;
	cout << "  -s setup_file    # Includes process and voltage info." << endl;
	cout
			<< "  -d [output_directory]    # Stored in current directory by default."
			<< endl;
	cout
			<< "  -o [output_file]   # Output file, in the same name as the input_file by default"
			<< endl << endl;
}

